/**
 * Created with JetBrains PhpStorm.
 * User: cgp
 * Date: 13-4-10
 * Time: 下午3:22
 */

var baseUrl = 'call/pppp.php',
    pid = parseInt($("input[name='pid']").val()),
    did = parseInt($("input[name='did']").val());

jQuery(document).ready(function($){
    $("span.btn_primary").click(function(){
	if(!$(".js_title").val()){
		alert("请先填写标题");
		return false;
	}
	if(!$(".editTextarea").val()){
		alert("请先填写正文");
		return false;
	}
	if(!$(".js_url").val()){
		alert("请先填原文链接");
		return false;
	}
        var mainTitle = $("input[name='mainTitle']"),
            that = $(this),
            step = $(this).attr('data-step') || 1,
            landid = parseInt($("#mainForm").find("input[name='lastid']").val());
        var postData = $("form#mainForm").serialize(),btnTxt = $(this).text();
		$.ajax({
		
            type:'POST',url: baseUrl,data: postData, dataType:'json',
            beforeSend:function(){
                that.text('保存中...').attr('disabled','disabled');
            },
            timeout: 10000})
            .done(function(result){
				
                if(result.status=='ok') {
                    if(!landid) {
                        location.href=uriPath+"?sid="+result.lastid+"&step=2";
                    } else {
                        alert(result.msg);
                    }
                } else{
                    alert(result.msg);
                }
                that.text(btnTxt).removeAttr('disabled');
            }) 
    });
    //图片上传
    $("#BtnUpload").bind('click',function(){
        if(!$("input#imgup").val().length) return;
        $("#upload_mth").val("1");
        $("form#mainForm").submit();
        $("#waitingUpload").html('<img src="images/loading.gif" alt="图片上传中"/>图片上传中..').show();
    });
    
    $(".BtnUpload").bind('click',function(){
        index = $(".BtnUpload").index($(this));
        if(!$("input.imgup").eq(index).val().length) return;
        $("#upload_mth").val(index);
        $("input.imgup").removeAttr("name").eq(index).attr("name","upfile");
        $("form#mainForm").submit();
        $(".waitingUpload").eq(index).html('<img src="images/loading.gif" alt="图片上传中"/>图片上传中..').show();
    });


});
